package com.jf.service.impl;

import com.jf.bean.Teacher;
import com.jf.dao.TeacherDao;
import com.jf.dao.impl.TeacherDaoImpl;
import com.jf.service.TeacherService;
import com.jf.util.PageInfo;
import com.jf.util.ResultJson;

import java.util.List;

public class TeacherServiceImpl implements TeacherService {
    private TeacherDao teacherDao = new TeacherDaoImpl();
    @Override
    public ResultJson getPage(PageInfo pageInfo, Teacher teacher) {
        List<Teacher> list = teacherDao.getPage(pageInfo, teacher);
        int count = teacherDao.getCount(teacher);
        pageInfo.setPageData(list,count);
        return ResultJson.success(pageInfo);
    }

    @Override
    public ResultJson saveOrUpdate(Teacher teacher) {
        int result;
        if(teacher.getId() == null){
            result = teacherDao.addSelect(teacher);
        }else{
            result = teacherDao.updateSelect(teacher);
        }
        return result > 0 ?ResultJson.success():ResultJson.error("操作失败");
    }

    @Override
    public ResultJson delete(int[] ids) {
        int result = teacherDao.batchDelete(ids);
        return result == ids.length ? ResultJson.success():ResultJson.error("删除异常");
    }

    @Override
    public ResultJson info(int id) {
        return ResultJson.success(teacherDao.getById(id));
    }

    @Override
    public ResultJson getList() {
        return ResultJson.success(teacherDao.getList());
    }
}
